﻿using MvcPaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zichan.DAL;
using zichan.Entity;

namespace zichan.BLL
{
    public class CabinetClassService
    {
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string AddCabinetClass(CabinetClass model)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                try
                {
                    if (db.CabinetClasses.Count(o => o.Purpose == model.Purpose) > 0)
                        return model.Purpose + "已存在";
                    db.CabinetClasses.Add(model);
                    db.SaveChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return "添加失败，请重试添加";
                }
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string EditCabinetClass(CabinetClass model)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                if (db.CabinetClasses.Count(o => o.Id != model.Id && o.Purpose == model.Purpose) > 0)
                    return model.Purpose + "已存在";
                db.Entry(model).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
                return string.Empty;
            }
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string RemoveCabinetClass(int id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                CabinetClass m = db.CabinetClasses.FirstOrDefault(o => o.Id == id);
                if (m != null)
                {
                    //if (db.PutawayServers.Count(o => o.PurposeId == id) > 0)
                    //{
                    //    return m.Purpose + "不能删除，已在使用中";
                    //}
                    db.CabinetClasses.Remove(m);
                    db.SaveChanges();
                    ClassLogService.AddLog("机柜用途分类", "删除" + "\"" + m.Purpose + "\"");
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取单条数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static CabinetClass GetCabinetClass(int Id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                return db.CabinetClasses.FirstOrDefault(o => o.Id == Id);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="controller"></param>
        /// <param name="action"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public static IPagedList<CabinetClass> GetCabinetClassList(string name, int pageSize, int pageIndex)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                IQueryable<CabinetClass> w = db.CabinetClasses;
                if (!string.IsNullOrEmpty(name))
                    w = w.Where(o => o.Purpose.Contains(name));

                List<CabinetClass> list = w.OrderBy(o => o.Sort).ThenBy(o => o.Id).Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
                foreach (CabinetClass c in list)
                {
                    //c.UseCount = db.PutawayServers.Count(o => o.PurposeId == c.Id);
                }
                return list.OrderBy(o=>o.Sort).ThenBy(o => o.Id).ToPagedList(pageIndex - 1, pageSize, w.Count());
            }
        }
        public static List<CabinetClass> List()
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                IQueryable<CabinetClass> w = db.CabinetClasses;
                return w.ToList();
            }
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="nameList">输入的名称集合</param>
        /// <param name="existName">重复的名称</param>
        /// <param name="nameListForLog">保存入数据库的名称，用以做日志使用</param>
        /// <returns></returns>
        public static string AddBatchClass(List<string> nameList, out List<string> existName, out List<string> nameListForLog)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                try
                {
                    existName = new List<string>();
                    nameListForLog = new List<string>();
                    int maxSort = db.CabinetClasses.Max(o => o.Sort);
                    List<CabinetClass> classList = new List<CabinetClass>();
                    foreach (string n in nameList)
                    {
                        if (db.CabinetClasses.Count(o => o.Purpose == n) > 0||classList.Count(o => o.Purpose == n) > 0)
                        {
                            existName.Add(n);
                            continue;
                        }
                        nameListForLog.Add(n);
                        CabinetClass model = new CabinetClass();
                        maxSort += 1;
                        model.Purpose = n;
                        model.Sort = maxSort;
                        classList.Add(model);
                    }
                    if (classList.Count() > 0)
                    {
                        db.CabinetClasses.AddRange(classList);
                        db.SaveChanges();
                    }
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    existName = new List<string>();
                    nameListForLog = new List<string>();
                    return "无法添加，请重试";
                }
            }
        }
        public static int MaxSort()
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                if (db.CabinetClasses.Count() > 0)
                    return db.CabinetClasses.Max(o => o.Sort);
                else return 0;
            }
        }
    }
}
